package com.souku.finance.application.http.retrofit;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.souku.finance.application.utils.level.FileUtils;
import com.souku.finance.application.utils.level.UIUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/19.
 */
public abstract class BaseDownloadCallback implements Callback<ResponseBody> {

    @Override
    public void onResponse(final Call<ResponseBody> call, Response<ResponseBody> response) {
        final ResponseBody body = response.body();
        new Thread() {
            @Override
            public void run() {
                final String path = writeResponseBodyToDisk(body);
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if (path != null) {
                            UIUtils.showToastSafe("下载成功");
                            onSuccess(path);
                        } else {
                            UIUtils.showToastSafe("下载失败");
                            onError(call);
                        }
                    }
                });
            }
        }.start();
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        onError(call);
    }

    public abstract void onSuccess(String path);

    public abstract void onError(Call<ResponseBody> call);

    //保存文件到指定路径
    private String writeResponseBodyToDisk(ResponseBody body) {
        try {

            String path = FileUtils.getCacheDir();
            path += "soukujinrong.apk";
            File futureStudioIconFile = new File(path);

            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);

                    if (read == -1) {
                        break;
                    }

                    outputStream.write(fileReader, 0, read);

                    fileSizeDownloaded += read;
                }

                outputStream.flush();

                return path;
            } catch (Exception e) {
                return null;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (Exception e) {
            return null;
        }
    }
}
